package com.yoso.shop.infrastructure.base.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yoso.shop.infrastructure.base.dao.BaseMapper;
import com.yoso.shop.infrastructure.base.entity.BaseEntity;
import com.yoso.shop.infrastructure.base.page.SearchParam;
import com.yoso.shop.infrastructure.base.pojo.PagePojo;
import com.yoso.shop.infrastructure.base.vo.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhizejin
 */
@Slf4j
public abstract class AbstractBaseService<E extends BaseEntity,M extends BaseEntityExample> implements IBaseService<E,M>{
    @Autowired(required = false)
    private M example;
    @Autowired(required = false)
    private BaseMapper<E,M> baseMapper;
    public abstract M getEntityExample(Map<String,String> paramMap);
    @Override
    public long countByExample(M example) {
        return this.baseMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(M example) {
        return this.baseMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return this.baseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(E record) {
        if (StringUtils.isBlank(record.getId())) {
//            增加
            record.setIdAuto();
            return this.baseMapper.insert(record);
        }else {
//            修改
            return this.baseMapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public int insertSelective(E record) {
        if (StringUtils.isBlank(record.getId())) {
//            增加
            record.setIdAuto();
            return this.baseMapper.insertSelective(record);
        }else {
//            修改
            return this.baseMapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public List<E> selectByExample(M example) {
        return this.baseMapper.selectByExample(example);
    }

    @Override
    public E selectByPrimaryKey(String id) {
        return this.baseMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(E record, M example) {
        return this.baseMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(E record, M example) {
        return this.baseMapper.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(E record) {
        return this.baseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(E record) {
        return this.baseMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<E> pageConditionsList(PageVo pageVo) {
        if (ObjectUtils.isEmpty(pageVo) || ObjectUtils.isEmpty(pageVo.getPagePojo())){
            return new PageInfo<>(this.baseMapper.selectByExample(null));
        }
        PagePojo pagePojo = pageVo.getPagePojo();
        List<SearchParam> searchParamList = pageVo.getSearchParamList();
        Map<String, String> paramMap = handlerParam(searchParamList);
        M entityExample = this.getEntityExample(paramMap);
        if (!ObjectUtils.isEmpty(pagePojo)){
            PageHelper.startPage(pagePojo.getCurPage(), pagePojo.getPageSize());
        }
        List<E> list = this.selectByExample(entityExample);
        return new PageInfo<>(list);
    }
    public Map<String, String> handlerParam(List<SearchParam> searchParamList) {
        return searchParamList.parallelStream()
                .collect(Collectors.toMap(SearchParam::getKey,SearchParam::getValue,(o1, o2)->o1));
    }

    @Override
    public List<E> listAll() {
        return this.baseMapper.selectByExample(null);
    }
}
